//
//  selectElementSubtypeView.m
//  dataGathering
//
//  Created by apple on 14-11-7.
//  Copyright (c) 2014年 shuili. All rights reserved.
//
#import "selectCollectModeView.h"
#import "selectElementSubtypeView.h"
#import "MapMainView.h"

@interface selectElementSubtypeView ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@end

@implementation selectElementSubtypeView
@synthesize tableData;
NSInteger selectSubType = 0;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [_backButton setImage:[UIImage imageNamed:@"u163_mouseOver.png"] forState:UIControlStateHighlighted];
    [_titleLabel setText:_titleName];
    ((UITableViewController*)([[self childViewControllers] objectAtIndex:0])).tableView.delegate = self;
    ((UITableViewController*)([[self childViewControllers] objectAtIndex:0])).tableView.dataSource = self;;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if(![segue identifier]){
        return;
    } else if([[segue identifier] compare:@"toSelectCollectModeView"] == NSOrderedSame){
        ((selectCollectModeView*)[segue destinationViewController]).titleName= [(LocalLayerModel*)[tableData objectAtIndex:selectSubType] name];
        ((selectCollectModeView*)[segue destinationViewController]).collectedDataType= [(LocalLayerModel*)[tableData objectAtIndex:selectSubType] type];
        NSLog(@"%@",[(LocalLayerModel*)[tableData objectAtIndex:selectSubType] type]);
    }
}


- (IBAction)backButtonClicked:(id)sender {
    [((UINavigationController*)(self.parentViewController)) popViewControllerAnimated:YES];
}

- (IBAction)closeButtonClicked:(id)sender {
    
    for (UIView* next = [[self navigationController].parentViewController.view superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            [((MapMainView*)nextResponder) removeNewDataWindow];
        }
    }
}

#pragma mark - UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 288, 48)];

    NSUInteger row = [indexPath row];

    NSString *appDocumentPath = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [appDocumentPath stringByAppendingString:@"/LayerIcons/"];
    //NSString *iconPath = [filePath stringByAppendingString:[(LocalLayerModel*)[tableData objectAtIndex:row] icon]];
    UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@""]];
    //UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"u141.png"]];
    view.frame = CGRectMake(8, 6, 32 , 32);
    view.contentMode = UIViewContentModeScaleAspectFit;
    
    UILabel *typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(64, 6, 200, 19)];
    [typeLabel setText:[(LocalLayerModel*)[tableData objectAtIndex:row] name]];
    
    [cell.contentView addSubview:view];
    [cell.contentView addSubview:typeLabel];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectSubType = [indexPath row];
    
    for (UIView* next = [[self navigationController].parentViewController.view superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            [((MapMainView*)nextResponder) setCurrentType:[(LocalLayerModel*)[tableData objectAtIndex:[indexPath row]] name]];
            [((MapMainView*)nextResponder) setMainType:_titleName];
        }
    }
    [self performSegueWithIdentifier:@"toSelectCollectModeView" sender:self];
}



@end
